﻿using System;
using Eneta.Portal.Common;
using Eneta.Portal.Deployment.Fields;
using Microsoft.SharePoint;

namespace Eneta.Portal.Deployment.Features
{
    public class FieldsReceiver : SPFeatureReceiver
    {
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPWeb web = null;
            if (properties.Feature.Definition.Scope == SPFeatureScope.Site)
            {
                SPSite site = properties.Feature.Parent as SPSite;
                web = site.RootWeb;
            }
            else
            {
                web = properties.Feature.Parent as SPWeb;
            }
            if (web == null)
            {
                //Logger.LogInfo("FeatureReceiver web object in null", "Eneta.Portal.Site.FieldReceiver");
                return;
            }
            if (properties.Feature.Properties.Count == 0)
            {
                return;
            }
            SPFeatureProperty property = properties.Feature.Properties["FileDef"];
            SPFeatureProperty parentWeb = properties.Feature.Properties["ParentWeb"];
            if (property == null)
                return;
            if (parentWeb == null)
                InitializeFields(property.Value, web);
            else
                InitializeFields(property.Value, web.Webs[parentWeb.Value]);
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            
        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
            
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
            
        }

        private void InitializeFields(string fileName, SPWeb web)
        {
            SPSecurity.RunWithElevatedPrivileges(
                delegate
                {
                    using (SPSite site = new SPSite(web.Site.ID))
                    {
                        using (SPWeb currentWeb = site.OpenWeb(web.ID))
                        {
                            SPDocumentLibrary assets = currentWeb.Lists["DeploymentAssets"] as SPDocumentLibrary;
                            SPQuery query = new SPQuery();
                            string queryString = "<Where><Eq><FieldRef Name='FileLeafRef'/><Value Type='Text'>{0}</Value></Eq></Where>";
                            query.Query = string.Format(queryString, fileName);
                            SPListItemCollection items = assets.GetItems(query);
                            if (items.Count == 0)
                            {
                                Logger.LogInfo("File is not found", "FieldsReceiver_InitializeFields");
                                return;
                            }
                            SPFile file = items[0].File;
                            FieldCollection fieldCollection = FieldCollection.Deserialize(file.OpenBinary());
                            foreach (Field field in fieldCollection.Fields)
                            {
                                if (!string.IsNullOrEmpty(field.WebName))
                                {
                                    web.EnsureCrossSiteLookup(field.WebName, field.ListId, field.InternalName);
                                }
                                else
                                {
                                    web.EnsureLookupField(field.ListId, field.InternalName);
                                }
                            }
                        }
                    }
                });
        }
    }
}
